#include <stdint.h>
//#include "dbg.h"


#define IO_TIMEOUT	100000	/* ms */

#define CA_IO_READ 0x01
#define CA_IO_WRITE 0x02
#define CA_IO_RESP 0x03

#define CA_CMD_DO 0xd0
#define CA_CMD_DI 0xd1
#define CA_CMD_ESAM 0xea
#define MAX_ESAM_DATA_LEN	512

#define MAX_CHAN_DI	6
#define MAX_CHAN_DO	6

enum {
	LNK_ID_DI0,
	LNK_ID_DO0 = 64,
	LNK_ID_ESAM = 128,
	LNK_ID_MAX = 256,
};

typedef struct _ca_io_t {
	uint32_t type;
	uint32_t cmd;
	uint32_t len;
	uint8_t ptr[0];
} ca_io_t;

typedef struct _ca_dio_t {
	uint32_t ch; 
	uint32_t val;
} ca_do_t, ca_di_t;
#if 0
typedef struct _ca_esam_t {
	uint8_t data[MAX_ESAM_DATA_LEN];
} ca_esam_t;
#else
#define ca_esam_t void
#endif
int ca_set_do(int ch, int val);
int ca_get_do(int ch, int *data);
int ca_get_di(int ch, int *data);

int ca_read_esam(unsigned char *buffer, unsigned int len, unsigned int *rlen, unsigned int timeout);
int ca_write_esam(unsigned char *data, unsigned int len);
int ca_reset_esam(int cmd);

int _io_write(ca_io_t *pio, int len);
int _io_read(ca_io_t *pio, int len, unsigned int timeout);

int _link_get_id(void *arg);
int _link_poll(int fd, unsigned int timeout);
int _link_write(int fd, void *data, int size);
int _link_read(int fd, void *data, int size);
int _link_open(int id);
void _link_close(int fd);
int _link_create(void);

int _parse_id_from_io(void *ptr);

